package leetcode_week.leetcode_week2.question2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

class Solution {
    public int score(String[] cards, char x) {
        ArrayList<String> xCards = new ArrayList<>();
        HashMap<Character, Integer> mapList1 = new HashMap<>();
        HashMap<Character, Integer> mapList2 = new HashMap<>();
        int count10 = 0;

        for (String card : cards) {
            if (getCount(card, x) == 2) {
                count10++;
            }
            if (getCount(card, x) == 1) {
                mapList2.put(card.charAt(0), mapList2.getOrDefault(card.charAt(0), 0) + 1);
            }
            if (getCount(card, x) == 0) {
                mapList1.put(card.charAt(1), mapList1.getOrDefault(card.charAt(1), 0) + 1);
            }
        }
        Collection<Integer> values = mapList1.values();
        Collection<Integer> values1 = mapList2.values();
        ArrayList<Integer> list1 = new ArrayList<>(values);
        ArrayList<Integer> list2 = new ArrayList<>(values1);
//            按降序排序
        list1.sort((a, b) -> b - a);
        list2.sort((a, b) -> b - a);
//           多个
        int n = list1.size();
        int m = list2.size();
        int score = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                if (list1.get(i) == 0) {
                    break;
                }
                if (list1.get(i) >= list1.get(j)) {
                    score += list1.get(j);
                    list1.set(i, list1.get(i) - list1.get(j));
                    list1.set(j, 0);
                }
                if (list1.get(i) < list1.get(j)) {
                    score += list1.get(i);
                    list1.set(j, list1.get(j) - list1.get(i));
                    list1.set(i, 0);
                    break;
                }
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = i+1; j < m; j++) {
                if (list2.get(i) == 0) {
                    break;
                }
                if (list2.get(i) >= list2.get(j)) {
                    score += list2.get(j);
                    list2.set(i, list2.get(i) - list2.get(j));
                    list2.set(j, 0);
                }
                if (list2.get(i) < list2.get(j)) {
                    score += list2.get(i);
                    list2.set(j, list2.get(j) - list2.get(i));
                    list2.set(i, 0);
                    break;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            if (list1.get(i) != 0) {
                if (count10 >= list1.get(i)) {
                    score += list1.get(i);
                    count10 -= list1.get(i);
                } else {
                    score += count10;
                    count10 = 0;
                }
            }
        }
        for (int i = 0; i < m; i++) {
            if (list2.get(i) != 0) {
                if (count10 >= list2.get(i)) {
                    score += list2.get(i);
                    count10 -= list2.get(i);
                } else {
                    score += count10;
                    count10 = 0;
                }
            }
        }
        return score;
    }

    int getCount(String card, char x) {
        if (card.charAt(0) == x && card.charAt(1) == x) return 2;
        if (card.charAt(1) == x) return 1;
        if (card.charAt(0) == x) return 0;
        return -1;
    }

    public static void main(String[] args) {
//        ["ab","aa","ab","bc","cc","bc","bb","ac","bc","bc","aa","aa","ba","bc","cb","ba","ac","bb","cb","ac","cb","cb","ba","bc","ca","ba","bb","cc","cc","ca","ab","bb","bc","ba","ac","bc","ac","ac","bc","bb","bc","ac","bc","aa","ba","cc","ac","bb","ba","bb"]
        String[] cards = {"ab", "aa", "ab", "bc", "cc", "bc", "bb", "ac", "bc", "bc", "aa", "aa", "ba", "bc", "cb", "ba", "ac", "bb", "cb", "ac", "cb", "cb", "ba", "bc", "ca", "ba", "bb", "cc", "cc", "ca", "ab", "bb", "bc", "ba", "ac", "bc", "ac", "ac", "bc", "bb", "bc", "ac", "bc", "aa", "ba", "cc", "ac", "bb", "ba", "bb"};
        char x = 'b';
        Solution solution = new Solution();
        System.out.println(solution.score(cards, x));
    }

}